﻿using System;
using HttpApiGateway.Interface;
using RpcCacheClient.Interface;

namespace HttpApiGateway.Idempotent.Token
{
    [RpcClient.Attr.UnityName("Memcached")]
    [RpcClient.Attr.ClassLifetimeAttr(RpcClient.Attr.ClassLifetimeType.单例)]
    internal class MemcachedKeyService : IKeyIdempotent
    {
        private readonly IMemcachedCache _Memcached;
        private readonly IIdempotentConfig _Config;
        public MemcachedKeyService(IMemcachedCache memcached, IIdempotentConfig config)
        {
            _Memcached = memcached;
            _Config = config;
        }
        public StatusSaveType SaveType => StatusSaveType.Memcached;


        public void Dispose()
        {

        }

        public bool SubmitToken(string tokenId)
        {
            string key = "Idempotent_" + tokenId;
            return !_Memcached.Add(key, tokenId, new TimeSpan(0, 0, _Config.Expire));
        }
    }
}
